﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ROrganizer.UI.Models;

namespace ROrganizer.UI.Forms.Core
{
    internal static class Formatter
    {
        public static short? GetRatingGroup(short? value)
        {
            return (short?)(value / 10);
        }

        public static string FormatRating(short? value)
        {
            if (value == null)
                return String.Empty;

            return String.Format("{0}.{1}", value / 10, value % 10);
        }

        public static string FormatRatingGroup(short? value)
        {
            if (value == null)
                return "[Unrated]";

            return value.ToString() + "+";
        }

        public static string FormatPublishedGroup(int? year)
        {
            if (year == null)
                return "[Undefined]";

            return year.ToString();
        }

        public static long GetSizeGroup(long size)
        {
            if (size < 200 * 1024 * 1024)
                return 200 * 1024 * 1024;

            if (size < 500 * 1024 * 1024)
                return 500 * 1024 * 1024;

            if (size < 1024 * 1024 * 1024)
                return 1024 * 1024 * 1024;

            if (size < 4L * 1024 * 1024 * 1024)
                return 4L * 1024 * 1024 * 1024;

            return 4L * 1024 * 1024 * 1024 + 1;
        }

        public static string FormatSize(long size)
        {
            if (size < 1024)
                return size + " B";

            if (size < 1024 * 1024)
                return (size / 1024M).ToString("F2") + " kB";

            if (size < 1024 * 1024 * 1024)
                return (size / 1024M / 1024M).ToString("F2") + " MB";

            return (size / 1024M / 1024M / 1024M).ToString("F2") + " GB";
        }

        public static string FormatSizeGroup(long size)
        {
            if (size == 200 * 1024 * 1024)
                return Formatter.FormatSize(size) + " and smaller";

            if (size == 500 * 1024 * 1024)
                return "Between " + Formatter.FormatSize(200 * 1024 * 1024) + " and " + Formatter.FormatSize(size);

            if (size == 1024 * 1024 * 1024)
                return "Between " + Formatter.FormatSize(500 * 1024 * 1024) + " and " + Formatter.FormatSize(size);

            if (size == 4L * 1024 * 1024 * 1024)
                return "Between " + Formatter.FormatSize(1024 * 1024 * 1024) + " and " + Formatter.FormatSize(size);

            return Formatter.FormatSize(4L * 1024 * 1024 * 1024) + " and bigger";
        }

        public static string FormatRuntime(int runtime)
        {
            if (runtime == 0)
                return String.Empty;

            var hours = runtime / 3600;
            var minutes = runtime / 60 % 60;
            var seconds = runtime % 60;

            return String.Format("{0}:{1:00}:{2:00}", hours, minutes, seconds);
        }

        public static int? GetResolutionGroup(Resolution resolution)
        {
            return resolution.Width;
        }

        public static string FormatResolution(Resolution resolution)
        {
            if (!resolution.IsSpecified())
                return String.Empty;

            return resolution.Width.Value + "x" + resolution.Height.Value;
        }

        public static string FormatResolutionGroup(int? width)
        {
            if (width == null)
                return "[Unknown]";

            switch (width.Value)
            {
                case 1920: return "1920 Full HD";
                case 1600: return "1600 HD+";
                case 1280: return "1280 HD";
                case 720: return "720 DVD";
                case 704: return "704 DVD";
            }

            return width.ToString();
        }

        public static string FormatPercent(double value)
        {
            return String.Format("{0:F2}%", 100 * value);
        }
    }
}
